package cn.controlsys; /**
 * @Project: study
 * @Title: WipmMd5.java
 * @Package cn.md5
 * @Description: TODO
 * @author gaobitian joslyn.bt717@qq.com
 * @date 2015-12-17
 * @Copyright: 2015 www.controlsys.cn Inc. All rights reserved.
 * @version V1.0
 */


import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @Package cn.md5
 * @ClassName: WipmMd5
 * @Description: @author gaobitian joslyn.bt717@qq.com
 * @date 2015-12-17
 * @Copyright: 2015 www.controlsys.cn Inc. All rights reserved.
 * @version V1.0
 *
 */
public class WIPMAPMD5 {
	
	private final static String AP_KEY = "808818"; //AP密码
	private final static String WIPM_KEY = "252138"; //设备连接密码
	
	/** 
	 * @Title: returnMD5 
	 * @Description: 加密sn
	 * @param @param sn
	 * @param @return
	 * @return String 
	 * @throws
	 * @author gaobitian joslyn.bt717@qq.com
	 * @date 2015-12-21
	*/
	public String returnMD5(String sn){
		return returnMD5(sn, AP_KEY);
	}
	
	/** 
	 * @Title: returnMD5 
	 * @Description: 通过key方式加密sn
	 * @param @param sn
	 * @param @param pwd
	 * @param @return
	 * @return String 
	 * @throws
	 * @author gaobitian joslyn.bt717@qq.com
	 * @date 2015-12-21
	*/
	public String returnMD5(String sn,String key){
		if(sn != null && sn.length() > 0 && key != null && key.length() > 0){
			StringBuilder input = new StringBuilder();
			for (int i = 0,count = sn.length(); i < count; i++) {
				char c = sn.charAt(i);
				if(c >= '0' && c<= '9'){
					input.append(c);
				}
			}
			return MD5(input.toString(), key);
		}
		return null;
	}
	
	private String MD5(String sn,String key){
		try {
			MessageDigest digest = MessageDigest.getInstance("md5");
			String s = key + sn;
			byte[] input = s.getBytes("utf-8");
			for (int i = 0,count = input.length; i < count; i++) {
				input[i] = (byte) (255-input[i]);
			}
			byte b ;
			for (int i = 0,count = input.length / 2; i < count; i++) {
				b = input[i];
				input[i] = input[input.length - 1 - i];
				input[input.length - 1 - i] = b;
			}
			byte[] md5_data = digest.digest(input);
			for (int i = 0,count = md5_data.length; i < count; i++) {
				md5_data[i] = (byte) (255- md5_data[i]);
			}
			byte[] b1 = new byte[4];
			byte[] b2 = new byte[4];
			for (int i = 0; i < 4; i++) {
				b1[i] = (byte) (md5_data[i] ^ md5_data[i + 4]);
				b2[i] = (byte) (md5_data[i + 8] ^ md5_data[i + 12]);
			}
			StringBuilder str = new StringBuilder(4);
			String hex = null;
			for (int i = 0; i < 4; i++) {
				hex = Integer.toHexString((byte)(b1[i] ^ b2[i]) & 0xFF);
				if(hex.length() == 1){
					hex = '0' + hex;
				}
				str.append(hex);
			}
			return str.toString();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return null;
	}
	private final static char[] codeSequence = {'1', '2', '3', '4', '5',
		'6', '7', '8', '9' ,'A' , 'B','C','D','E','F'};
	
	public static void main1(String[] args) {
		long startlong = System.currentTimeMillis();
		WIPMAPMD5 md = new WIPMAPMD5();
//		Random random = new Random();
		long l = 20025000;
		String sn = null;
		for (int i = 0; i < 15000; i++) {
			sn = "0"+l;
			System.out.println(sn+","+md.returnMD5(sn));
			l++;
		}
//		for (int i = 0; i < 10000000; i++) {
//			String s = "";
//			for (int j = 0; j < 130; j++) {
//				s += codeSequence[random.nextInt(15)];
//			}
//			System.out.println(s);
////			System.out.println(md.returnMD5(s, "252138"));
//		}
		long endlong = System.currentTimeMillis();
//		System.out.println("耗时"+(endlong-startlong)+"毫秒");
	}
	
	public static void main(String[] args) {
		WIPMAPMD5 m = new WIPMAPMD5();
		System.out.println(m.returnMD5("020025364"));
	}
}
